from flask import Blueprint, request, jsonify
from views.mydb import db1
from utils.acl import acl
from utils.myredis import r
from utils.mytoken import token1
from views.comany import get_courses
from utils.rbac import rbac

import json
bp_user = Blueprint("user",__name__)



@bp_user.route("/login")
def login():
    user = request.args.get("user")
    pwd = request.args.get("pwd")
    sql = "select * from user where name='%s'and password='%s'"%(user,pwd)
    res = db1.find(sql)
    if res:
        vurl = acl.get_page_reslist(res["id"])
        key = "interface"+str(res["id"])
        r().insert_values(key,vurl)
        token = token1.encode_jwt(int(res["id"]),60*60,"123")
        return jsonify({"code":200,"userid":res["id"],"token":token,"vurl":vurl})
    else:
        return jsonify({"code":200,"msg":'ok'})


@bp_user.route("/getResource")
def getResource():
    uid= 1
    res = acl.get_resourcelist(uid,1)
    res1 = rbac.get_user_resouce(uid,1)
    res.extends(res1)
    ser = get_courses(res)
    return jsonify({"list":ser})



@bp_user.route("/roleM",methods=['GET','POST','PUT','DELETE'])
def roleM():
    return "sdadsdas"

@bp_user.route("/test1")
def test1():
    key = "abac"
    r.hset_key(key,"ip","1270.0.1")
    r.hset_key(key,"level","003")
    r.hset_key(key,"time","['2021-10-10','2021-12-10']")
    values = r.hget_keys(key)

    for (k,v) in values.items():
        if str(k,encoding='utf8')=='time':
            print(eval(v))
    return 'ok'






















